<?php
/**
 * Description of Login 获取集团公众号openid
 * @author rhett_ni<415080528@qq.com>
 */
namespace Home\Controller;
use Common\Extend\Weixin\TPWechat;
use Common\Extend\wxTsMsg;
use Think\Controller;

class LoginController extends Controller {

    public function index(){
        if(!cookie('wx')['openid']) {
            $return_uri = I("get.return_uri");
            $param = I("get.param");
            $wx = new TPWechat(C('WEIXIN_CONFIG'));
            if (empty($_GET['code'])) {
                $rurl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $url = $wx->getOauthRedirect($rurl, 'success');
                @header("Location: $url");
            } else {
                $result = $wx->getOauthAccessToken();
                if (empty($result['openid'])) {
                    echo $wx->errMsg;
                    echo $wx->errCode;
                }
                cookie('wx', $result);
            }
        }
        $this->display();
    }

    public function ajax_login(){
        $data = I();
        $url = 'http://api.258.com/WeiTang/bindOpenID';
        $post_data = [
            'username'=>$data['username'],
            'token'=>md5('_'.md5(substr(time(), 0,4)).'_WT21-G547-KB21-MO02-LP54'),
            'openId'=>cookie('wx')['openid'],
            'pwd'=>md5($data['pwd']),
        ];
        $res = getRemoteData($url,$post_data);
        $res = json_decode($res, true);
        $type = $res['type'] ? $res['type'] : 0;
        if($res['s']=='1') {
            output(['type'=>$type], 200, $res['info']);
        }else{
            output([], 201, $res['info']);
        }
    }
}
